﻿using System;
using Ebt.Mvvm;
using Windows.System;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows_8_MVVM.Controls;

namespace Windows_8_MVVM.ViewModels.Settings
{
    static class SettingsCharmLauncher
    {
        private static Func<RootModel> _getRootModelCallback;

        static SettingsCharmLauncher()
        {
            SettingsPane.GetForCurrentView().CommandsRequested += OnSettingsPaneCommandsRequested;
        }

        public static void Initialize(Func<RootModel> getRootModelCallback)
        {
            if (getRootModelCallback == null)
                throw new ArgumentNullException("getRootModelCallback");

            _getRootModelCallback = getRootModelCallback;
        }

        public static RootModel RootModel { get { return _getRootModelCallback(); } }

        private static void OnSettingsPaneCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            args.Request.ApplicationCommands.Add(new SettingsCommand("AboutPane", "About", OnAboutPaneOpening));
            args.Request.ApplicationCommands.Add(new SettingsCommand("PrivacyStatement", "Privacy Statement", OnOpenPrivacyStatement));
        }

        private static void OnAboutPaneOpening(IUICommand command)
        {
            SettingsCharmManager.ShowPane(new AboutPane());
        }

        private static async void OnOpenPrivacyStatement(IUICommand command)
        {
            var uri = new Uri("http://w8mvvm.codeplex.com/license", UriKind.Absolute);
            await Launcher.LaunchUriAsync(uri);
        }
        
    }
}
